package com.supcon.exchangedata.service.hj;

import com.supcon.exchangedata.pojo.entity.oracle.BaseGeoArea;
import com.supcon.exchangedata.pojo.entity.oracle.BaseGeoAreaDetail;
import com.supcon.exchangedata.pojo.entity.oracle.GisPoint;
import com.supcon.exchangedata.pojo.entity.mysql.operation.hj.HjCleanComplexStation;
import com.supcon.exchangedata.repository.master.MasterBaseAreaRepository;
import com.supcon.exchangedata.repository.master.MasterBaseGeoAreaDetailRepository;
import com.supcon.exchangedata.repository.master.MasterGisPointRepository;
import com.supcon.exchangedata.repository.slave6.SlaveHjBasicInfo1;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @program: exchange-data
 * @description: 减量综合体
 * @author: cherubr.Sumin
 * @create: 2021-12-20 14:00
 **/
@Slf4j
@Service
public class GarbageStationService {


    @Autowired
    private SlaveHjBasicInfo1 slaveHjBasicInfo1;

    @Autowired
    private MasterBaseAreaRepository masterBaseAreaRepository;
    @Autowired
    private MasterGisPointRepository masterGisPointRepository;

    @Resource
    private MasterBaseGeoAreaDetailRepository masterBaseGeoAreaDetailRepository;


    @Transactional(rollbackFor = Exception.class)
    public void transGarbageStation() throws Exception {
        int count = 0;

        List<HjCleanComplexStation> all = slaveHjBasicInfo1.findAll();

        List<BaseGeoArea> removeList = new ArrayList<>();
        List<BaseGeoArea> updateList = new ArrayList<>();

        List<BaseGeoArea> baseGeoAreasByAreaType = masterBaseAreaRepository.findBaseGeoAreaByAreaType(4011L);

        if (!CollectionUtils.isEmpty(baseGeoAreasByAreaType)) {
            baseGeoAreasByAreaType.forEach((e) -> {
                if (!CollectionUtils.isEmpty(all)) {
                    Iterator<HjCleanComplexStation> iterator = all.iterator();
                    boolean removeFlag = true;
                    while (iterator.hasNext()) {
                        HjCleanComplexStation next = iterator.next();
                        if (next.getCleanStationName().equals(e.getAreaName())) {
                            BaseGeoAreaDetail baseGeoAreaDetail = new BaseGeoAreaDetail();
                            baseGeoAreaDetail.setSubsidiaryInformation1(next.getCleanStationCapacity());
                            baseGeoAreaDetail.setSubsidiaryInformation2(next.getCleanStationStorageCapacity());
                            String cleanStationOperationTime = next.getCleanStationOperationTime();
                            if (!StringUtils.isEmpty(cleanStationOperationTime)) {
                                String[] split = cleanStationOperationTime.split("-");
                                baseGeoAreaDetail.setSubsidiaryInformation3(split[0]);
                                baseGeoAreaDetail.setSubsidiaryInformation4(split[1]);
                            }

                            e.setBaseGeoAreaDetail(baseGeoAreaDetail);
                            removeFlag = false;
                            e.setAreaCode("hjjlzhta" + next.getId());
                            e.setUpdateDate(LocalDateTime.now());
                            String cleanStationLongitudeLatitud = next.getCleanStationLongitudeLatitud();
                            if (!StringUtils.isEmpty(cleanStationLongitudeLatitud)) {
                                String[] split = cleanStationLongitudeLatitud.split(",");
                                if (split.length == 2) {
                                    e.setLongitude(Double.valueOf(split[0]));
                                    e.setLatitude(Double.valueOf(split[1]));
                                }
                            }

                            updateList.add(e);
                            iterator.remove();
                            break;
                        }
                    }
                    if (removeFlag) {
                        removeList.add(e);
                    }
                }
            });
        }


        deleteAreaAndPoint(removeList);
        updateAreaAndPointByGarbageStation(updateList);

        if (!CollectionUtils.isEmpty(all)) {
            int i = 0;
            for (HjCleanComplexStation station : all) {
                BaseGeoArea area = new BaseGeoArea();
                i++;
                area.setAreaCode("hjjlzhta" + station.getId());
                area.setParentId(20919L);
                area.setAreaType(4011L);
                area.setAreaName(station.getCleanStationName());
                area.setFullLevelName("环境→减量综合体→" + station.getCleanStationName());
                area.setUpdateDate(LocalDateTime.now());
                area.setUpdator("jpa-program");
                String cleanStationLongitudeLatitud = station.getCleanStationLongitudeLatitud();
                if (!StringUtils.isEmpty(cleanStationLongitudeLatitud)) {
                    String[] split = cleanStationLongitudeLatitud.split(",");
                    if (split.length == 2) {
                        area.setLongitude(Double.valueOf(split[0]));
                        area.setLatitude(Double.valueOf(split[1]));
                    }
                }
                BaseGeoArea save = masterBaseAreaRepository.save(area);

                if(area.getLongitude() != null && area.getLatitude() != null){
                    GisPoint gisPoint = new GisPoint();
                    gisPoint.setYwCategory("4011");
                    gisPoint.setCode("hjjlzhta" + station.getId());
                    gisPoint.setObjType(1L);
                    gisPoint.setName(station.getCleanStationName());
                    gisPoint.setObjCode("hjjlzhta" + station.getId());
                    gisPoint.setBusinessCategory("4011");
                    gisPoint.setObjId(save.getId());
                    masterGisPointRepository.save(gisPoint);
                }
                BaseGeoAreaDetail baseGeoAreaDetail = new BaseGeoAreaDetail();
                baseGeoAreaDetail.setAreaId(save.getId());
                baseGeoAreaDetail.setSubsidiaryInformation1(station.getCleanStationCapacity());
                baseGeoAreaDetail.setSubsidiaryInformation2(station.getCleanStationStorageCapacity());
                String cleanStationOperationTime = station.getCleanStationOperationTime();
                if (!StringUtils.isEmpty(cleanStationOperationTime)) {
                    String[] split = cleanStationOperationTime.split("-");
                    baseGeoAreaDetail.setSubsidiaryInformation3(split[0]);
                    baseGeoAreaDetail.setSubsidiaryInformation4(split[1]);
                }
                masterBaseGeoAreaDetailRepository.save(baseGeoAreaDetail);
            }

        }


//        throw new Exception("回滚");
    }

    /**
     * 删除不需要的图层点位信息
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteAreaAndPoint(List<BaseGeoArea> removeList) {
        if (!CollectionUtils.isEmpty(removeList)) {
            removeList.forEach((e) -> {
                masterBaseAreaRepository.deleteById(e.getId());
                masterGisPointRepository.deleteByObjIdAndYwCategory(e.getId(), String.valueOf(e.getAreaType()));
                masterBaseGeoAreaDetailRepository.deleteByAreaId(e.getId());
            });
        }
    }

    /**
     * 更新减量综合体相关图层信息
     *
     * @param updateList
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateAreaAndPointByGarbageStation(List<BaseGeoArea> updateList) {
        if (!CollectionUtils.isEmpty(updateList)) {
            updateList.forEach((e) -> {
                e.setParentId(20919L);
                e.setUpdateDate(LocalDateTime.now());
                e.setUpdator("jpa-program");
                masterBaseAreaRepository.save(e);
                //更新gisPoint
                String areaName = e.getAreaName();
                if (e.getLongitude() != null) {
                    GisPoint distinctByObjId = masterGisPointRepository.findDistinctByObjId(e.getId());
                    if (distinctByObjId == null) {
                        distinctByObjId = new GisPoint();
                    }
                    distinctByObjId.setYwCategory(String.valueOf(e.getAreaType()));
                    distinctByObjId.setObjId(e.getId());
                    distinctByObjId.setBusinessCategory(String.valueOf(e.getAreaType()));
                    distinctByObjId.setObjCode(e.getAreaCode());
                    distinctByObjId.setCode(e.getAreaCode());
                    distinctByObjId.setLatitude(e.getLatitude());
                    distinctByObjId.setLongitude(e.getLongitude());
                    distinctByObjId.setRoad(e.getAddress());
                    distinctByObjId.setName(e.getAreaName());
                    distinctByObjId.setObjType(1L);
                    masterGisPointRepository.save(distinctByObjId);
                }
                BaseGeoAreaDetail distinctByAreaId = masterBaseGeoAreaDetailRepository.findDistinctByAreaId(e.getId());
                if (distinctByAreaId == null) {
                    distinctByAreaId = new BaseGeoAreaDetail();
                }
                BaseGeoAreaDetail baseGeoAreaDetail = e.getBaseGeoAreaDetail();
                if (baseGeoAreaDetail != null) {
                    distinctByAreaId.setSubsidiaryInformation1(baseGeoAreaDetail.getSubsidiaryInformation1());
                    distinctByAreaId.setSubsidiaryInformation2(baseGeoAreaDetail.getSubsidiaryInformation2());
                    distinctByAreaId.setSubsidiaryInformation3(baseGeoAreaDetail.getSubsidiaryInformation3());
                    distinctByAreaId.setSubsidiaryInformation4(baseGeoAreaDetail.getSubsidiaryInformation4());
                }
                distinctByAreaId.setAreaId(e.getId());
                masterBaseGeoAreaDetailRepository.save(distinctByAreaId);

            });
        }
    }


}
